package diceng;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 如何使用中断标识停止线程？
 * 第一种：使用volatile关键字 下面的 m1()
 * 第二种：使用atomicBoolean 下面的 m2()
 * 第三种：通过Thread类自带的中断api方法实现
 * 下面的mm();//演示中断状态的改变
 * m4演示中断标识设为true后，并不会立刻stop程序
 */
public class InterruptDemo {
    static volatile boolean isStop = false;
    static AtomicBoolean atomicBoolean = new AtomicBoolean(false);
    public static void main(String[] args) {
        /** public static boolean interrupted
         *  静态方法 Thread.interrupted();
         *  判断线程是否被中断，并清除当前中断状态，类似i++
         *  这个方法做了两件事：
         *  1.返回当前线程的中断状态
         *  2.将当前线程的中断状态设为false
         */
        System.out.println(Thread.currentThread().getName()+"--1-"+Thread.interrupted());//false
        System.out.println(Thread.currentThread().getName()+"--2-"+Thread.interrupted());//false
        System.out.println("11111");
        Thread.currentThread().interrupt();//---false--true
        System.out.println("22222");
        System.out.println(Thread.currentThread().getName()+"--3-"+Thread.interrupted());//true
        System.out.println(Thread.currentThread().getName()+"--4-"+Thread.interrupted());//false
    }

    private static void m5() {
        Thread t1 =new Thread (()->{
            while (true){
                if (Thread.currentThread().isInterrupted()){
                    System.out.println("-----isInterrupted() = true,程序结束");
                    break;
                }
                try { Thread.sleep(500); } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();//线程中断标志位为false，无法停下，需要再次调interrupt()设置为true
                    e.printStackTrace();
                }//加了这个停不下来
//                在3秒后报java.lang.InterruptedException: sleep interrupted，并且停不下来
                System.out.println("----hello interrupt()");
            }
        },"t1");
        t1.start();
//        暂停线程几秒钟
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
        new Thread(()->{
            t1.interrupt();//修改t1的中断标识位为true
        },"t2").start();
    }

    private static void m4() {
        //演示中断标志为true后，并不会立刻stop程序
        Thread t1 =  new Thread(()->{
             for (int i = 1; i <=300; i++) {
                 System.out.println("-------i:"+i);
             }
                 System.out.println("t1.interrupt()调用之后02:"+Thread.currentThread().isInterrupted());
         },"t1");
        t1.start();
        System.out.println("t1.interrupt()调用之前，中断标识默认值："+t1.isInterrupted());
        //暂停1ms，我的cpu性能太好了，3ms看不到效果
        try { TimeUnit.MILLISECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
//       实例方法interrupt()仅仅是设置线程的中断状态位置为true，不会停止线程
        t1.interrupt();
//        活动状态，t1线程还在执行中
        System.out.println("t1.interrupt()调用之后01:"+t1.isInterrupted());
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
//        非活动状态，t1线程不在执行中，已经结束执行了
        System.out.println("t1.interrupt()调用之后03:"+t1.isInterrupted());
    }

    private static void mm() {
        Thread t1 =new Thread (()->{
            while (true){
                if (Thread.currentThread().isInterrupted()){
                    System.out.println("-----t1线程被中断了， break，程序结束");
                    break;
                }
                System.out.println("----hello ");
            }
        },"t1");
        t1.start();
        System.out.println("11************"+t1.isInterrupted());
//        暂停线程1ms
        try { TimeUnit.MILLISECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
        t1.interrupt();//true
        System.out.println("00*************"+t1.isInterrupted());
    }

    private static void m3() {
        Thread t1 =new Thread (()->{
            while (true){
                if (Thread.currentThread().isInterrupted()){
                    System.out.println("-----isInterrupted() = true,程序结束");
                    break;
                }
                System.out.println("----hello interrupt()");
            }
        },"t1");
        t1.start();
//        暂停线程几秒钟
        try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
        new Thread(()->{
            t1.interrupt();//修改t1的中断标识位为true
        },"t2").start();
    }

    private static void m2() {
        new Thread (()->{
            while (true){
                if (atomicBoolean.get()){
                    System.out.println("-----atomicBoolean.get() = true,程序结束");
                    break;
                }
                System.out.println("----hello atomicBoolean");
            }
        },"t1").start();
//        暂停线程几秒钟
        try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
        new Thread(()->{
            atomicBoolean.set(true);
        },"t2").start();
    }

    private static void m1() {
        new Thread (()->{
            while (true){
                if (isStop){
                    System.out.println("-----isStop = true,程序结束");
                    break;
                }
                System.out.println("----hello isStop");
            }
        },"t1").start();
//        暂停线程几秒钟
        try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
        new Thread(()->{
            isStop = true;
        },"t2").start();
    }
}
